package com.qianclass.talklive.app.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

import com.qianclass.talklive.app.InterfaceBack.OnDragFrameLayoutClickListener;

/**
 * Author:  Administrator-Li
 * Date:    2017/7/12 14:14
 * TODO:    
 */

public class DragFrameLayout extends FrameLayout
{

    private int lastX;
    private int lastY;
    private boolean bigWindow;
    private int mMeasuredWidth;
    private int mMeasuredHeight;
    private long mTouchTime;
    private int mMarginLeft;
    private int mMarginTop;
    private OnDragFrameLayoutClickListener mDragFrameLayoutClickListener;

    public DragFrameLayout(Context context) {
        super(context);
    }

    public DragFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DragFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        View parent = (View) getParent();
        int parentMeasuredWidth = parent.getMeasuredWidth();
        int parentMeasuredHeight = parent.getMeasuredHeight();
        int measuredWidth = getMeasuredWidth();
        int measuredHeight = getMeasuredHeight();
        int left;
        int top;
        if (!bigWindow) {
            mMeasuredWidth = getMeasuredWidth();
            mMeasuredHeight = getMeasuredHeight();
            mMarginLeft = getLeft();
            mMarginTop = getTop();
        }

        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                lastX = (int) event.getRawX();
                lastY = (int) event.getRawY();

                long currentTimeMillis = System.currentTimeMillis();

                if (currentTimeMillis - mTouchTime > 200) {
                    mTouchTime = currentTimeMillis;
                } else {
                    bigWindow = !bigWindow;

                    mDragFrameLayoutClickListener.onDragFrameLayoutClick(bigWindow);

                    FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams)getLayoutParams();
                    if (bigWindow) {
                        layoutParams.width = parentMeasuredWidth;
                        layoutParams.height = parentMeasuredHeight;
                        layoutParams.leftMargin =0;
                        layoutParams.topMargin =0;
                    } else {
                        layoutParams.width = mMeasuredWidth;
                        layoutParams.height = mMeasuredHeight;
                        layoutParams.leftMargin =mMarginLeft;
                        layoutParams.topMargin =mMarginTop;
                    }
                    setLayoutParams(layoutParams);
                }
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                break;
            case MotionEvent.ACTION_POINTER_UP:
                break;
            case MotionEvent.ACTION_MOVE:
                int dx = (int) event.getRawX() - lastX;
                int dy = (int) event.getRawY() - lastY;

                if (getLeft() + dx < 0) {
                    left = 0;
                }else if(getLeft() + dx > parentMeasuredWidth - measuredWidth){
                    left = parentMeasuredWidth - measuredWidth;
                }else{
                    left = getLeft() + dx;
                }

                if (getTop() + dy < 0) {
                    top = 0;
                } else if (getTop() + dy > parentMeasuredHeight - measuredHeight) {
                    top = parentMeasuredHeight - measuredHeight;
                } else {
                    top = getTop() + dy;
                }

                //Log.e("offset", left + "||" + top);

                FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams)getLayoutParams();
                layoutParams.leftMargin =left;
                layoutParams.topMargin =top;
                setLayoutParams(layoutParams);
                lastX = (int) event.getRawX();
                lastY = (int) event.getRawY();
                break;
        }
        invalidate();
        return true;
    }

    /*@Override
    public boolean onTouchEvent(MotionEvent event) {
        // 获取当前触摸的绝对坐标
        int rawX = (int) event.getRawX();
        int rawY = (int) event.getRawY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // 上一次离开时的坐标
                lastX = rawX;
                lastY = rawY;
                break;
            case MotionEvent.ACTION_MOVE:
                // 两次的偏移量
                int offsetX = rawX - lastX;
                int offsetY = rawY - lastY;
                moveView(offsetX, offsetY);
                // 不断修改上次移动完成后坐标
                lastX = rawX;
                lastY = rawY;
                break;
            default:
                break;
        }
        return true;
    }

    private void moveView(int offsetX, int offsetY) {
        // 方法一
        // layout(getLeft() + offsetX, getTop() + offsetY, getRight() +
        // offsetX, getBottom() + offsetY);

        // 方法二
        // offsetLeftAndRight(offsetX);
        // offsetTopAndBottom(offsetY);

        // 方法三
        // LinearLayout.LayoutParams layoutParams = (LayoutParams)
        // getLayoutParams();
        // layoutParams.leftMargin = getLeft() + offsetX;
        // layoutParams.topMargin = getTop() + offsetY;
        // setLayoutParams(layoutParams);

        // 方法四
        // ViewGroup.MarginLayoutParams layoutParams = (MarginLayoutParams)
        // getLayoutParams();
        // layoutParams.leftMargin = getLeft() + offsetX;
        // layoutParams.topMargin = getLeft() + offsetY;
        // setLayoutParams(layoutParams);

        // 方法五
        ((View) getParent()).scrollBy(-offsetX, -offsetY);
    }*/

    public void setOnDragFrameLayoutClickListener(OnDragFrameLayoutClickListener listener){
        mDragFrameLayoutClickListener = listener;
    }
}
